package com.zds.hj.poseidon.biz.common.helper;

import com.alibaba.fastjson.JSON;
import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.hj.competency.facade.management.dto.protocol.*;
import com.zds.hj.competency.facade.management.enums.protocol.ProtocolSignKeyTypeEnum;
import com.zds.hj.competency.facade.mapping.dto.syxapi.UploadSignInfoDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.sign.OpenApiResponseDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.sign.RequestPerCreateAccountDto;
import com.zds.hj.poseidon.biz.common.CompetencyServiceInterface;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wex
 * @Description 协议填充处理
 * @Date 2023/6/8 15:54
 */
@Service
@Slf4j
public class ProcessAgreementHandleService {

    @Resource
    private CompetencyServiceInterface competencyServiceInterface;


    /**
     * 协议填充处理
     *
     * @param params   引擎模块中的 param
     * @param batchNo  批次号
     * @param fillData 填充的数据
     * @return
     */
    public ProtocolInsRespDto agreementFillHandle(BaseReqModel params, String batchNo, Map<String, Object> fillData) {
        // 模块中配置的协议
        String jsonObject = params.getDispatchModuleNodes().getExtParams().get("NODE_VALID_PROTOCOL_INFO");
        List<ProtocolBizAttributeDto> vaildProtocolInfoList = JSON.parseArray(jsonObject, ProtocolBizAttributeDto.class);
        log.info("有效的协议信息为：{}", vaildProtocolInfoList);
        if (EmptyChecker.isEmpty(vaildProtocolInfoList)) {
            return new ProtocolInsRespDto();
        }
        // 协议填充 入参准备
        ProtocolFillReqDto protocolFillReqDto = new ProtocolFillReqDto();
        protocolFillReqDto.setBatchNo(batchNo);
        protocolFillReqDto.setProtocolBizInfo(vaildProtocolInfoList);
        protocolFillReqDto.setFillInfo(fillData);
        ProtocolInsRespDto protocolFillInsDto = competencyServiceInterface.protocolFill(protocolFillReqDto);
        //排序后，返回协议信息
        protocolFillInsDto.setProtocolIns(protocolFillInsDto.getProtocolIns().stream().sorted(Comparator.comparing(ProtocolInsDto::getShowOrder)).collect(Collectors.toList()));
        return protocolFillInsDto;
    }


    /**
     * 企业法人开户
     * @param applyInfoDto
     * @return
     */
    public String getCfcaId(CreditInfoEntityDto applyInfoDto) {
        RequestPerCreateAccountDto reqDto = new RequestPerCreateAccountDto();
        reqDto.setName(applyInfoDto.getName());
        // 证件号码
        reqDto.setCardNo(applyInfoDto.getCardId());
        // 手机号
        reqDto.setBankReservePhone(applyInfoDto.getPhone());
        // 银行卡账号
        reqDto.setBankAccountNo(applyInfoDto.getCardNo());
        OpenApiResponseDto responseDto = competencyServiceInterface.personalOpenChapter(reqDto);
        log.info("CFCA法人开户响应：{}", responseDto);
        if (EmptyChecker.isEmpty(responseDto) || EmptyChecker.isEmpty(responseDto.getUserId())) {
            throw new BusinessException( "CFCA法人开户失败");
        }
        return responseDto.getUserId();
    }

    /**
     * 协议签章
     * @param applyInfo
     */
    public void agreementSignHandle(CreditInfoEntityDto applyInfo) {
        // 个人信息
        final String perCfcaIsOpen = getCfcaId(applyInfo);
        Map<ProtocolSignKeyTypeEnum, UploadSignInfoDto> signInfoMap = new HashMap<>();
        //法人
        String legalNameByChainEnt = applyInfo.getName();
        UploadSignInfoDto legalSignInfoDto = new UploadSignInfoDto();
        legalSignInfoDto.setUserId(perCfcaIsOpen);
        legalSignInfoDto.setEnterpriseName(legalNameByChainEnt);
        signInfoMap.put(ProtocolSignKeyTypeEnum.ENTERPRISE_LEGAL, legalSignInfoDto);
        // 协议签章
        ProtocolSignReqDto protocolSignReqDto = new ProtocolSignReqDto();
        // 签章 必传批次号
        protocolSignReqDto.setBatchNo(applyInfo.getBatchNo());
        protocolSignReqDto.setLocation("127.0.0.1");
        protocolSignReqDto.setSignInfoMap(signInfoMap);
        competencyServiceInterface.protocolSignature(protocolSignReqDto);
    }

}
